<?php

namespace App\Http\Controllers;

use App\Article;
use Illuminate\Http\Request;
// use Illuminate\Support\Facades\Redis;


class ArticleController extends Controller
{

  /**
   * @OA\Info(
   *     version="1.0",
   *     title="博客系统的swagger-ui，包括增删改查，详情页",
   * )
   */

    /**
     * @OA\Get(
     *     path="/article",
     *     summary="查询所有文章信息",
     *     @OA\Response(response="200", description="An example resource"),
     *     tags={"文章管理"},
     * )
     */
    public function index()
    {

        $articles = Article::all();
        return $articles;
    }

    public function hello()
    {
        return "hello laravel";
    }

    /**
     * @OA\Get(
     *     path="/article/{id}",
     *     summary="根据 ID 获取文章信息",
     *     @OA\Parameter(name="id", in="path", @OA\Schema(type="string"), required=true, description="文章ID"),
     *     @OA\Response(response="200", description="An example resource"),
     *     tags={"文章管理"},
     * )
     */
    public function show($id)
    {
        //
        $article = Article::find($id);
        return $article;
    }


    /**
     * @OA\Post(
     *     path="/article",
     *     summary="添加文章",
     *     @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 @OA\Property(
     *                     property="title",
     *                     type="string"
     *                 ),
     *                 @OA\Property(
     *                     property="content",
     *                     type="string"
     *                 ),
     *                 example={"title": "我是标题", "content": "我是内容"}
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response=200,
     *         description="OK"
     *     ),
     *    tags={"文章管理"},
     * )
     */
    public function store(Request $request)
    {
        //
        $input = $request->all();
        $user = Article::create($input);
        if($user) {
            return response()->json([
              'data' => $user,
              'code' => 200,
              'msg'  => '创建成功'
          ]);
        } else {
            return response()->json([
              'code' => 500,
              'msg'  => false
          ]);
        }
    }


    /**
     * @OA\Put(
     *     path="/article/{id}",
     *     summary="修改文章",
     *     @OA\Parameter(name="id", in="path", @OA\Schema(type="integer"), required=true, description="文章ID"),
     *     @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 @OA\Property(
     *                     property="title",
     *                     type="string"
     *                 ),
     *                 @OA\Property(
     *                     property="content",
     *                     type="string"
     *                 ),
     *                 example={"title": "我是修改后的标题", "content": "我是修改后的内容"}
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response=200,
     *         description="OK"
     *     ),
     *    tags={"文章管理"},
     * )
     */
    public function update(Request $request, $id)
    {
        //
        $content = $request->input('content');
        $title = $request->input('title');

        $flight = Article::find($id);
        $flight->content = $content;
        $flight->title = $title;
        $isres = $flight->save();
        return response()->json([
          'data' => $flight,
          'code' => 200,
          'msg'  => '修改成功'
        ]);
    }

    /**
     * @OA\Delete(
     *     path="/article/{id}",
     *     summary="根据 ID 删除单篇文章",
     *     @OA\Parameter(name="id", in="path", @OA\Schema(type="integer"), required=true, description="文章ID"),
     *     @OA\Response(response="200", description="An example resource"),
     *     tags={"文章管理"},
     * )
     */
    public function destroy($id)
    {
        //
        $flight = Article::find($id);
        $isres = $flight->delete();
        return response()->json([
          'data' => $flight,
          'code' => 200,
          'msg'  => '删除成功'
      ]);
    }
}
